package com.invengo.module_robot.ui.fragment.bookinfo;

import android.app.Application;

import androidx.annotation.NonNull;

import com.invengo.base.http.RobotRxUtils;
import com.invengo.library_orionstar.ActionEvent;
import com.invengo.module_robot.data.entity.SearchBean;
import com.invengo.module_robot.data.repsitory.RobotRepsitory;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.StringUtils;

public class BookInfoViewModel extends BaseViewModel<RobotRepsitory> {
    public SingleLiveEvent<String> author = new SingleLiveEvent<>();
    public SingleLiveEvent<String> isbn = new SingleLiveEvent<>();
    public SingleLiveEvent<String> bookName = new SingleLiveEvent<>();
    public SingleLiveEvent<String> publishing = new SingleLiveEvent<>();
    public SingleLiveEvent<String> content = new SingleLiveEvent<>();
    public SingleLiveEvent<Boolean> isOpen = new SingleLiveEvent<>();
    public SingleLiveEvent<String> tipContent = new SingleLiveEvent<>();

    public SingleLiveEvent<String> image = new SingleLiveEvent<>();

    public SingleLiveEvent<Boolean> showOpenButton = new SingleLiveEvent<>();

    public BookInfoViewModel(@NonNull Application application) {
        super(application, RobotRepsitory.getInstance());
        isOpen.postValue(false);
    }

    UIChangeObservable ui = new UIChangeObservable();


    public class UIChangeObservable {
        public SingleLiveEvent<Boolean> openCloseEvent;
        public SingleLiveEvent<Boolean> backEvent;

        public SingleLiveEvent<List<String>> locationEvent = new SingleLiveEvent<>();


        public UIChangeObservable() {
            openCloseEvent = new SingleLiveEvent<>();
            backEvent = new SingleLiveEvent<>();
        }
    }

    public void initData(String isbn, String image, String content, String bookName, String author, String publisher) {
        author=   author.isEmpty()|author.equals("null")?"":author;
        publisher=   publisher.isEmpty()|publisher.equals("null")?"":publisher;
        this.author.postValue(author);
        this.publishing.postValue(publisher);



        this.isbn.postValue(isbn);
        this.bookName.postValue(bookName);
//        this.content.postValue(content);
        this.image.postValue(image);
        getLocations(isbn);
    }

    /**
     * 获取位置
     *
     * @param isbn ISBN
     */
    public void getLocations(String isbn) {
        model.getBookInfoByIsbn(isbn)
                .subscribeOn(Schedulers.io())
                .compose(RobotRxUtils.applySchedulers(getLifecycleProvider(), this, true))
                .map((Function<SearchBean, List<String>>) searchBean -> {
                    List<String> locations = new ArrayList<>();
                    if (searchBean.getItems() != null) {
                        String publishValue = searchBean.getItems().get(0).getBookInfo().getPublisher();
                        String contentValue = searchBean.getItems().get(0).getBookInfo().getSummary();
                        String authorValue = searchBean.getItems().get(0).getBookInfo().getAuthor();
                        publishValue=   publishValue.isEmpty()|publishValue.equals("null")?"":publishValue;
                        contentValue=   contentValue.isEmpty()|contentValue.equals("null")?"":contentValue;
                        authorValue=   authorValue.isEmpty()|authorValue.equals("null")?"":authorValue;
                        this.publishing.postValue(publishValue);
                        this.content.postValue(contentValue);
                        this.author.postValue(authorValue);

                        for (SearchBean.OneItemsDTO item : searchBean.getItems()) {
                            for (SearchBean.OneItemsDTO.ItemsDTO itemItem : item.getItems()) {
                                locations.add(itemItem.getItem().getLocationName());
                            }
                        }
                    }
                    return locations;
                })
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    getUC().getHideRobotLoadingEvent().call();
                })
                .subscribe(list -> {
                    ui.locationEvent.postValue(list);
                });
    }


    /**
     * 打开-关闭
     */
    public BindingCommand openClose = new BindingCommand(() -> {
        isOpen.postValue(!isOpen.getValue());
        ui.openCloseEvent.postValue(isOpen.getValue());
    });

    /**
     * 回去
     */
    public BindingCommand goBack = new BindingCommand(() -> {
        getUC().getOnBackPressedEvent().call();
    });
    /**
     * 下水总管
     */
    public BindingCommand launchMain = new BindingCommand(() -> {
        getUC().getFinishActivityEvent().call();
    });

    /**
     * 动作命令
     */
    public BindingCommand<ActionEvent> actionCommand = new BindingCommand<>(action -> {
        Observable.just(action)
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(event -> {
                    switch (action.getEvent()) {

                        case ActionEvent.ACTION_DOWN:
//                            move2Next();
                            break;
                        case ActionEvent.ACTION_UP:
//                            move2Pre();
                            break;
                        case ActionEvent.ACTION_SEARCH:
                            break;
                    }
                });
    });

}
